package chain_user_config

import (
	"chain_bridge_service/internal/pkg/core"
	chian_to_chain "chain_bridge_service/internal/repository/mongodb/chain_to_chain"
	chain_user_config "chain_bridge_service/internal/repository/mongodb/chain_user_config"
	"github.com/pkg/errors"
)

type DeleteChainData struct {
	Id string
}

func (s *service) Delete(ctx core.Context, keyData *DeleteChainData) error {
	model := chain_user_config.NewModel()
	model.Id = keyData.Id
	data, err := model.GetInfoById(s.db.GetDb())
	if err != nil {
		return err
	}
	chainModel := chian_to_chain.NewModel()
	chainModel.UserId = data.UserId
	chainModel.ChainConfigId1 = data.ChainConfigId
	chainModel.ChainConfigId0 = data.ChainConfigId
	count, err := chainModel.GetCountByParams(s.db.GetDb())
	if err != nil {
		return err
	}
	if count != 0 {
		return errors.New("[无法删除,以配置该链信息]")
	}
	err = model.Delete(s.db.GetDb())
	return err
}
